package com.shortlink.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTimeUtils {
    public static Date getNow(){
        return new Date();
    }

    public static Date getYesterdayNow(){
        long currentTimeMillis=System.currentTimeMillis();
        long oneDayMillis = 24 * 60 * 60 * 1000;
        Date yesterdayNow=new Date(currentTimeMillis-oneDayMillis);
        return yesterdayNow;
    }

    public static Date getToday(){
//        long currentTimeMillis=System.currentTimeMillis();
//        long oneDayMillis = 24 * 60 * 60 * 1000;
//        long todayMillis=currentTimeMillis-(currentTimeMillis % (24 * 60 * 60 * 1000));
//        Date today= new Date(todayMillis);
//        return today;

        Calendar calendar = Calendar.getInstance();
        // 将小时、分钟、秒和毫秒都设置为0
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.HOUR_OF_DAY, -8);
        return calendar.getTime();
    }

    public static List<Date> getWeek(){
        long currentTimeMillis=System.currentTimeMillis();
        long oneDayMillis = 24 * 60 * 60 * 1000;
        long todayMillis=currentTimeMillis-(currentTimeMillis % (24 * 60 * 60 * 1000));
        Date now=new Date(currentTimeMillis);
        List<Date> weekDates=new ArrayList<>();
        Week week = DateUtil.dayOfWeekEnum(now);
        int weekValue = week.getValue()-2;
        long mondayMillis=todayMillis-oneDayMillis*weekValue;
        for (int i = 0; i < 7; i++) {
            Date date = new Date(mondayMillis + i * oneDayMillis);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR_OF_DAY, -8);
            weekDates.add(calendar.getTime());
        }
        return weekDates;
    }
}
